package com.stock.demo.function.home.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioGroup;

import com.stock.demo.R;
import com.stock.demo.framework.ui.activity.BaseActivity;

public class LiveBroadcastListActivity extends BaseActivity implements LiveBroadCastListView {

    private LiveBroadcastTypeFragment mPreviewFragment;
    private LiveBroadcastTypeFragment mLivingFragment;
    private LiveBroadcastTypeFragment mReviewFragment;
    private LiveBroadcastTypeFragment mMineFragment;
    private RadioGroup mRgLiveBroadcast;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_broadcast_list);
        initView();
        initData();
    }

    public static void startAction(Context context) {
        Intent intent = new Intent(context, LiveBroadcastListActivity.class);
        context.startActivity(intent);
    }

    private void initData() {
        initFragments();
    }

    private void initFragments() {
        Bundle previewBundle = new Bundle();
        previewBundle.putSerializable(LiveBroadcastTypeFragment.ARGUMENT_KEY_TYPE, LiveBroadcastTypeFragment.LiveType.PREVIEW);
        mPreviewFragment = new LiveBroadcastTypeFragment();
        mPreviewFragment.setArguments(previewBundle);

        Bundle livingBundle = new Bundle();
        livingBundle.putSerializable(LiveBroadcastTypeFragment.ARGUMENT_KEY_TYPE, LiveBroadcastTypeFragment.LiveType.LIVING);
        mLivingFragment = new LiveBroadcastTypeFragment();
        mLivingFragment.setArguments(livingBundle);

        Bundle reviewBundle = new Bundle();
        reviewBundle.putSerializable(LiveBroadcastTypeFragment.ARGUMENT_KEY_TYPE, LiveBroadcastTypeFragment.LiveType.REVIEW);
        mReviewFragment = new LiveBroadcastTypeFragment();
        mReviewFragment.setArguments(reviewBundle);

        Bundle mineBundle = new Bundle();
        mineBundle.putSerializable(LiveBroadcastTypeFragment.ARGUMENT_KEY_TYPE, LiveBroadcastTypeFragment.LiveType.MINE);
        mMineFragment = new LiveBroadcastTypeFragment();
        mMineFragment.setArguments(mineBundle);

        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_live_broadcast_fragment_container, mPreviewFragment)
                .add(R.id.fl_live_broadcast_fragment_container, mLivingFragment)
                .add(R.id.fl_live_broadcast_fragment_container, mReviewFragment)
                .add(R.id.fl_live_broadcast_fragment_container, mMineFragment)
                .hide(mLivingFragment)
                .hide(mReviewFragment)
                .hide(mMineFragment)
                .show(mPreviewFragment)
                .commit();
    }

    public void initView() {
        findViewById(R.id.iv_live_broadcast_finish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRgLiveBroadcast = (RadioGroup) findViewById(R.id.rg_live_broadcast);
        mRgLiveBroadcast.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction().hide(mPreviewFragment).hide(mLivingFragment).hide(mReviewFragment).hide(mMineFragment);
                switch (checkedId) {
                    case R.id.rb_live_broadcast_preview:
                        transaction.show(mPreviewFragment);
                        break;
                    case R.id.rb_live_broadcast_living:
                        transaction.show(mLivingFragment);
                        break;
                    case R.id.rb_live_broadcast_review:
                        transaction.show(mReviewFragment);
                        break;
                    case R.id.rb_live_broadcast_mine:
                        transaction.show(mMineFragment);
                        break;
                }
                transaction.commit();
            }
        });
    }

}
